//
//  ListenMoreViewController.m
//  瓜子电台
//
//  Created by lanou on 16/8/11.
//  Copyright © 2016年 YueZhang. All rights reserved.
//

#import "ListenMoreViewController.h"
#import "WMPageController.h"
#import "ListenMoreTableViewController.h"
#import "ListenMoreModel.h"
#import "TingShangHaiModel.h"
#import "ViwepagerModel.h"
#import "ListenMoreRecommendTableViewController.h"

@interface ListenMoreViewController ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSMutableArray *tingSHArray;

@property (nonatomic, strong) NSMutableArray *carouseArray;

@end

@implementation ListenMoreViewController



- (NSMutableArray *)tingSHArray
{
    if (!_tingSHArray) {
        _tingSHArray = [[NSMutableArray alloc] init];
    }
    return _tingSHArray;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];//这里注意，需要写在最后面，要不然上面的效果不会出现
    ListenMoreModel *model = [[ListenMoreModel alloc] init];
    model.keywordName = @"推荐";
    [self.dataArray insertObject:model atIndex:0];
    
    self.title = self.myTitle;
    self.menuHeight = 30; //导航栏高度
    
    self.menuBGColor = [UIColor whiteColor];
    self.menuViewStyle = WMMenuViewStyleLine;//这里设置菜单view的样式
    self.progressHeight = 2;//下划线的高度，需要WMMenuViewStyleLine样式
    self.progressColor = [UIColor redColor];//设置下划线(或者边框)颜色
    self.titleSizeSelected = 15;//设置选中文字大小
    self.titleColorSelected = [UIColor redColor];//设置选中文字颜色
    self.titleSizeNormal = 15;
    self.selectIndex = 0;
    [self requestData];
    
    if ([self.type isEqualToString:@"听"]) {
        self.title = @"听上海";
    }
    
}

- (NSArray *)titleWidth {
    if ([self.type isEqualToString:@"others"]) {
        NSMutableArray *widthArr = [NSMutableArray new];
        for (ListenMoreModel *model in self.dataArray) {
            CGFloat width = [model.keywordName boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine attributes:nil context:nil].size.width;
            [widthArr addObject:@(width + 40)];
        }
        return [widthArr copy];
    } else {
        NSMutableArray *widthArr = [NSMutableArray new];
        for (TingShangHaiModel *model in self.tingSHArray) {
            CGFloat width = [model.name boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine attributes:nil context:nil].size.width;
            [widthArr addObject:@(width + 40)];
        }
        return [widthArr copy];
    }
    
}



- (void)requestData
{
    if ([self.type  isEqual: @"others"]) {
        NSString *urlStr = @"http://mobile.ximalaya.com/mobile/discovery/v1/category/keywords?categoryId=1&device=iPhone&statEvent=pageview%2Fcategory%40%E5%90%AC%E8%B5%84%E8%AE%AF&statModule=%E5%90%AC%E8%B5%84%E8%AE%AF_%E6%9B%B4%E5%A4%9A&statPage=tab%40%E5%8F%91%E7%8E%B0_%E6%8E%A8%E8%8D%90&statPosition=2";
        NSString *detailStr = [urlStr stringByReplacingOccurrencesOfString:@"categoryId=1" withString:[NSString stringWithFormat:@"categoryId=%ld", self.categoryId]];
        [NetWorkrequestManage requestWithType:GET url:detailStr parameters:nil finish:^(NSData *data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            for (NSDictionary *dataDic in dict[@"keywords"]) {
                ListenMoreModel *model = [[ListenMoreModel alloc] init];
                [model setValuesForKeysWithDictionary:dataDic];
                [self.dataArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self.itemsWidths = [self titleWidth]; //每个 MenuItem 的宽度
                [self reloadData];
            });
        } error:^(NSError *error) {
            
        }];
    } else if ([self.type isEqualToString:@"听"]){
        NSString *str = @"http://mobile.ximalaya.com/mobile/discovery/v1/city/tab?code=43_310000_3100&device=iPhone&statEvent=pageview%2Falbumlist%40%E5%90%AC%E4%B8%8A%E6%B5%B7&statModule=%E6%9C%AC%E5%9C%B0%E5%90%AC_%E6%9B%B4%E5%A4%9A&statPage=tab%40%E5%8F%91%E7%8E%B0_%E6%8E%A8%E8%8D%90&statPosition=1&version=5.4.21";
        NSString *detailStr = [str stringByReplacingOccurrencesOfString:@"code=43_310000_3100" withString:[NSString stringWithFormat:@"code=%@", self.code]];
        [NetWorkrequestManage requestWithType:GET url:detailStr parameters:nil finish:^(NSData *data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            for (NSDictionary *data in dict[@"tab"]) {
                TingShangHaiModel *model = [[TingShangHaiModel alloc] init];
                [model setValuesForKeysWithDictionary:data];
                [self.tingSHArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                self.itemsWidths = [self titleWidth]; //每个 MenuItem 的宽度
                [self reloadData];
            });
        } error:^(NSError *error) {
            
        }];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    
    if ([self.type isEqualToString:@"others"]) {
        return self.dataArray.count;
    } else {
        return self.tingSHArray.count;
    }
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    if ([self.type isEqualToString:@"others"]) {
        if (index == 0) {
            ListenMoreRecommendTableViewController *vc = [[ListenMoreRecommendTableViewController alloc] init];
            vc.type = @"others";
            vc.categoryId = self.categoryId;
            return vc;
        } else {
            ListenMoreTableViewController *vc = [[ListenMoreTableViewController alloc] init];
            ListenMoreModel *model = self.dataArray[index];
            vc.keyWordId = model.keywordId;
            vc.categoryId = self.categoryId;
            vc.type = self.type;
            return vc;
        }
        
    } else {
        if (index == 0) {
            ListenMoreRecommendTableViewController *vc = [[ListenMoreRecommendTableViewController alloc] init];
            vc.type = @"听";
            vc.categoryId = self.categoryId;
            return vc;
        } else {
            ListenMoreTableViewController *vc = [[ListenMoreTableViewController alloc] init];
            TingShangHaiModel *model = self.tingSHArray[index];
            
            vc.myId = model.myId;
            vc.type = self.type;
            return vc;
        }
        
    }
    
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    if ([self.type isEqualToString:@"others"]) {
        ListenMoreModel *model = self.dataArray[index];
        return model.keywordName;
    } else {
        TingShangHaiModel *model = self.tingSHArray[index];
        return model.name;
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
